// 引入刚刚在建立连接mysql数据库的db.js文件
const db = require('../config/db');

// 引入Sequelize对象
const Sequelize = db.sequelize;
// 引入上一步的文章数据表模型文件
const mOvertime = Sequelize.import('../schema/overtime');
// 自动创建表
mOvertime.sync({force: false});

class overtimemodules {
    /**
     * 创建模型
     * @param data
     * @returns {Promise<*>}
     */
    static async createOvertime(data) {
        console.log(data);
        return await mOvertime.create({
            leaveid: data.DEFAULTDEPTID, 
           
        }).catch(e=>{ console.log(e);})
    }
    /**批量增加 */
    static async createOvertimes(data) {
        //console.log(data);
        return await mOvertime.bulkCreate(data,{ignoreDuplicates:true}).catch(e=>{ console.log(e,"异常");})
    }
    /**根据工号查询请假记录 */
    static async  getOvertimeByLoginName(name,start,end){
        return await mOvertime.findAll({
            attributes:{exclude:['suggestion','overtimeid','id','secondDEFAULTDEPTID','createdAt','updatedAt']},
            where:{
                SSN:name,
                startTIME:{ $between: [start, end]},
                endTIME:{ $between: [start, end]},
                state:'已结束'
                // CHECKTIME:{
                //     $between: [start, end]
                // }
            }
        });
    }
    //获得最新的数据
    static async getlast(){
        try {
            //Sequelize.query('SELECT updatedAt FROM leave_table ORDER BY id DESC LIMIT 1').then(res=>{console.log(JSON.stringify(res));})
            return await mOvertime.findAll({limit:1, order:Sequelize.literal('id DESC'),attributes:{include:['updatedAt']}});
        } catch (error) {
            console.log(error);
        }
    }
    
}

module.exports = overtimemodules